package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MatrBean;
import com.koron.css2.baseConfig.mapper.MatrMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.ValidationKey;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 材料价格-添加
 *
 * @author 廖大富
 */
public class MatrAdd implements ServerInterface {

    private static Logger logger = LogManager.getLogger(MatrAdd.class);

    @Override
    @ValidationKey(clazz = MatrBean.class,method = "insert")
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            MatrMapper mapper = factory.getMapper(MatrMapper.class);
            MatrBean bean = JsonUtils.objectToPojo(req.getData(), MatrBean.class);
            // 校验字段重复
            if (mapper.check("matr_no", bean.getMatrNo()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "材料编号为"+bean.getMatrNo()+"的材料已设置价格。", void.class);
            }
            bean.setCreateInfo(userInfo);
            mapper.insertMatr(bean);
        } catch (Exception e) {
            logger.error("材料价格添加失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "材料价格添加失败", void.class);
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
    }
}